package com.mql.ganio.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.mql.ganio.R;
import com.mql.ganio.model.response.GanIoResponse;
import com.mql.ganio.ui.base.baseadapter.BaseRecyclerViewAdapter;
import com.mql.ganio.ui.base.baseadapter.BaseRecyclerViewHolder;
import com.mql.ganio.util.TimeUtil;

import java.util.List;

/**
 * 作者：Administrator on 2017/11/9 18:08
 */
public class CustomAdapter extends BaseRecyclerViewAdapter<GanIoResponse.ResultsBean,BaseRecyclerViewHolder> {
    private boolean isAll = false;
    private List<GanIoResponse.ResultsBean> list;
    public void setAllType(boolean isAll) {
        this.isAll = isAll;
    }

    public CustomAdapter(Context context, List mList) {
        super(context, mList);
        this.list=mList;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_custom, parent, false);

        return new BaseRecyclerViewHolder(context,view);
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);

        if (isAll && "福利".equals(list.get(position).getType())) {
            holder.getView(R.id.iv_all_welfare).setVisibility(View.VISIBLE);
            holder.getView(R.id.ll_welfare_other).setVisibility(View.GONE);
            holder.setImageView(R.id.iv_all_welfare,list.get(position).getUrl());
        } else {
            holder.getView(R.id.iv_all_welfare).setVisibility(View.GONE);
            holder.getView(R.id.ll_welfare_other).setVisibility(View.VISIBLE);
        }

        if (isAll) {
          holder.getView(R.id.tv_content_type).setVisibility(View.VISIBLE);
            holder.setText(R.id.tv_content_type," · " + list.get(position).getType());
        } else {
            holder.getView(R.id.tv_content_type).setVisibility(View.GONE);

        }
        holder.setText(R.id.tv_android_des,list.get(position).getDesc());
        holder.setText(R.id.tv_android_who,list.get(position).getWho());
        holder.setText(R.id.tv_android_time, TimeUtil.getTranslateTime(list.get(position).getPublishedAt()));

        // 显示gif图片会很耗内存
        if (list.get(position).getImages() != null
                && list.get(position).getImages().size() > 0
                && !TextUtils.isEmpty(list.get(position).getImages().get(0))) {
//                binding.ivAndroidPic.setVisibility(View.GONE);
           holder.getView(R.id.iv_android_pic).setVisibility(View.VISIBLE);
            holder.setImageView(R.id.iv_android_pic,list.get(position).getImages().get(0));

        } else {
            holder.getView(R.id.iv_android_pic).setVisibility(View.GONE);
        }

        holder.getView(R.id.ll_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                WebViewActivity.loadUrl(v.getContext(), list.get(position).getUrl(), "加载中...");
            }
        });
    }

}
